package main

import (
	"code.google.com/p/gochess/movement"
	pos "code.google.com/p/gochess/position"
)

type GameState int32

const (
	Unfinnished GameState = iota
	Checkmate
	Stalemate
	ThreefoldRepetition
	CheckmateImpossible
)

var gameStates = map[GameState]string{
	Unfinnished:         "unfinnished",
	Checkmate:           "checkmate",
	Stalemate:           "stalemate",
	ThreefoldRepetition: "draw by threefold repetition",
	CheckmateImpossible: "checkmate impossible"}

func (this GameState) String() string {
	return gameStates[this]
}

const (
	HumanPlayer int32 = iota
	ComputerPlayer
)

type Game struct {
	opponents [pos.BiggestColor + 1]int32
	record    []pos.Move
	position  *pos.Position
}

func NewGame(position *pos.Position, white, black int32) *Game {

	return &Game{[pos.BiggestColor + 1]int32{pos.White: white, pos.Black: black},
		[]pos.Move{}, position}

}

func (this *Game) Player(color pos.Color) int32 {
	return this.opponents[color]
}

/*
	todo: ThreefoldRepetition	// must be claimed!
	todo: CheckmateImpossible
	todo: FIFTY_MOVE_RULE	// must be claimed!
returns checkmate if the color who is in turn (!) is checkmate */
// todo: this func is a work around
func (this *Game) Status() GameState {

	moves := movement.GenerateLegalMoves(this.position)

	if len(moves) == 0 {
		// there is no move left for player -> see what we have
		if movement.IsCheck(this.position, this.position.SideToMove()) {
			return Checkmate
		}
		return Stalemate
	}
	return Unfinnished
}
